import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { LawCaseCoreApi } from '#/api/law/caseCore';
import type { DescriptionItemSchema } from '#/components/description';

import { h } from 'vue';

import { formatDate, formatDateTime } from '@vben/utils';

import { Typography } from 'ant-design-vue';

import { z } from '#/adapter/form';
import { DictBadge } from '#/components/dict-badge';
import DictTag from '#/components/dict-tag/dict-tag.vue';
import { getRangePickerDefaultProps } from '#/utils';

const { Paragraph } = Typography;
/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'naturalYear',
      label: '所属年度',
      component: 'DatePicker',
      componentProps: {
        picker: 'year',
        format: 'YYYY',
        valueFormat: 'YYYY',
      },
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'naturalYm',
      label: '所属年月',
      component: 'InputNumber',
      componentProps: {
        min: 0,
        placeholder: '请输入所属年月',
      },
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'caseNo',
      label: '案号',
      rules: 'required',
      component: 'Input',
      componentProps: {
        placeholder: '请输入案号',
      },
    },
    {
      fieldName: 'caseCause',
      label: '案由',
      rules: 'required',
      component: 'Input',
      componentProps: {
        placeholder: '请输入案由',
      },
    },
    {
      fieldName: 'caseCauseCode',
      label: '案由编码（依据最高法案由规定）',
      component: 'Input',
      componentProps: {
        placeholder: '请输入案由编码（依据最高法案由规定）',
      },
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'courtName',
      label: '审理法院',
      rules: 'required',
      component: 'Input',
      componentProps: {
        placeholder: '请输入审理法院',
      },
    },
    {
      fieldName: 'courtCode',
      label: '法院编码',
      component: 'Input',
      componentProps: {
        placeholder: '请输入法院编码',
      },
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'judgmentDate',
      label: '裁判时间',
      rules: 'required',
      component: 'DatePicker',
      componentProps: {
        showTime: false,
        format: 'YYYY-MM-DD',
        valueFormat: 'YYYY-MM-DD',
      },
    },
    {
      fieldName: 'trialProcedure',
      label: '审理程序',
      rules: 'required',
      component: 'Select',
      componentProps: {
        options: [
          {
            label: '一审程序',
            value: '一审程序',
          },
          {
            label: '二审程序',
            value: '二审程序',
          },
          {
            label: '再审程序',
            value: '再审程序',
          },
          {
            label: '特别程序',
            value: '特别程序',
          },
          {
            label: '督促程序',
            value: '督促程序',
          },
          {
            label: '死刑复核',
            value: '死刑复核',
          },
          {
            label: '速裁程序',
            value: '速裁程序',
          },
          {
            label: '非诉执行审查',
            value: '非诉执行审查',
          },
          {
            label: '首次执行',
            value: '首次执行',
          },
          {
            label: '恢复执行',
            value: '恢复执行',
          },
          {
            label: '财产保全执行',
            value: '财产保全执行',
          },
          {
            label: '执行异议',
            value: '执行异议',
          },
        ],
        placeholder: '请选择审理程序',
      },
    },
    {
      fieldName: 'judgmentResult',
      label: '裁判结果',
      component: 'Textarea',
      componentProps: {
        rows: 6,
        placeholder: '请输入裁判结果',
      },
    },
    {
      fieldName: 'caseType',
      label: '案件类型',
      component: 'Select',
      componentProps: {
        options: [
          {
            label: '民事',
            value: '民事',
          },
          {
            label: '刑事',
            value: '刑事',
          },
          {
            label: '行政',
            value: '行政',
          },
          {
            label: '执行',
            value: '执行',
          },
        ],
        placeholder: '请选择案件类型',
      },
    },
    {
      fieldName: 'caseSource',
      label: '案件来源',
      component: 'Select',
      componentProps: {
        options: [
          {
            label: '裁判文书网',
            value: '裁判文书网',
          },
          {
            label: '法院系统',
            value: '法院系统',
          },
        ],
        placeholder: '请选择案件来源',
      },
    },
    {
      fieldName: 'caseLevel',
      label: '法院级别（最高法/高院/中院/基层）',
      component: 'Select',
      componentProps: {
        options: [
          {
            label: '最高法',
            value: '最高法',
          },
          {
            label: '高院',
            value: '高院',
          },
          {
            label: '中院',
            value: '中院',
          },
          {
            label: '基层',
            value: '基层',
          },
        ],
        placeholder: '请选择法院级别（最高法/高院/中院/基层）',
      },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'RadioGroup',
      componentProps: {
        options: [
          {
            label: '归档',
            value: '0',
          },
          {
            label: '有效',
            value: '1',
          },
        ],
        buttonStyle: 'solid',
        optionType: 'button',
      },
    },
    {
      fieldName: 'isPublished',
      label: '是否已公开',
      component: 'RadioGroup',
      componentProps: {
        options: [
          {
            label: '是',
            value: true,
          },
          {
            label: '否',
            value: false,
          },
        ],
        buttonStyle: 'solid',
        optionType: 'button',
      },
    },
    {
      fieldName: 'vectorProcessed',
      label: '是否向量化',
      component: 'RadioGroup',
      componentProps: {
        options: [
          {
            label: '是',
            value: true,
          },
          {
            label: '否',
            value: false,
          },
        ],
        buttonStyle: 'solid',
        optionType: 'button',
      },
    },
    {
      fieldName: 'aiProcessStatus',
      label: 'ai处理状态',
      rules: 'required',
      component: 'Select',
      componentProps: {
        options: [
          {
            label: '待处理',
            value: '0',
          },
          {
            label: '处理中',
            value: '1',
          },
          {
            label: '处理完成',
            value: '2',
          },
          {
            label: '处理失败',
            value: '3',
          },
        ],
        placeholder: '请选择ai处理状态',
      },
    },
    {
      fieldName: 'aiConfidence',
      label: 'AI分析置信度',
      component: 'InputNumber',
      componentProps: {
        min: 0,
        placeholder:
          '请输入AI分析置信度（如分类、相似度匹配的置信度，0-100.00)',
      },
    },
    {
      fieldName: 'aiCategoryTags',
      label: 'AI自动分类标签',
      component: 'Select',
      componentProps: {
        mode: 'tags',
        placeholder: '请输入AI自动分类标签',
      },
    },
    {
      fieldName: 'aiKeywords',
      label: 'AI关键词提取',
      component: 'Select',
      componentProps: {
        mode: 'tags',
        placeholder: '请输入AI关键词提取',
      },
    },
    {
      fieldName: 'aiKeySentences',
      label: 'AI提取的关键句',
      component: 'Select',
      componentProps: {
        mode: 'tags',
        placeholder: '请输入AI提取的关键句',
      },
    },
    {
      fieldName: 'aiAutoSummary',
      label: 'AI自动摘要',
      component: 'Textarea',
      componentProps: {
        rows: 6,
        placeholder: '请输入AI自动摘要',
      },
    },
    {
      fieldName: 'aiDispiteKeywords',
      label: 'AI争议焦点关键字',
      component: 'Select',
      componentProps: {
        mode: 'tags',
        placeholder: '请输入AI争议焦点关键字',
      },
    },
    {
      fieldName: 'aiLawArticleRecommend',
      label: 'AI法律条文推荐',
      component: 'Textarea',
      componentProps: {
        rows: 6,
        placeholder: '请输入AI法律条文推荐',
      },
    },
    {
      fieldName: 'aiRiskLevel',
      label: 'AI风险评估等级',
      component: 'Select',
      componentProps: {
        options: [
          {
            label: '无风险',
            value: '0',
          },
          {
            label: '低风险',
            value: '1',
          },
          {
            label: '中风险',
            value: '2',
          },
          {
            label: '高风险',
            value: '3',
          },
        ],
        placeholder: '请选择AI风险评估等级',
      },
    },
  ];
}

/** 导入表单的schema */
export function caseImportFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'file',
      label: '案件数据',
      component: 'Upload',
      rules: 'required',
      help: '仅允许导入 xls、xlsx 格式文件',
    },
    {
      fieldName: 'updateSupport',
      label: '是否覆盖',
      component: 'Switch',
      componentProps: {
        checkedChildren: '是',
        unCheckedChildren: '否',
      },
      rules: z.boolean().default(false),
      help: '是否更新已经存在的用户数据',
    },
  ];
}

/** 详情页字段 */
export function useDetailSchema(): DescriptionItemSchema[] {
  return [
    {
      label: '所属年度',
      field: 'naturalYear',
    },
    {
      label: '所属年月',
      field: 'naturalYm',
    },
    {
      label: '案号',
      field: 'caseNo',
    },
    {
      label: '案由',
      field: 'caseCause',
    },
    {
      label: '案由编码',
      field: 'caseCauseCode',
    },
    {
      label: '审理法院',
      field: 'courtName',
    },
    {
      label: '法院编码',
      field: 'courtCode',
    },
    {
      label: '裁判时间',
      field: 'judgmentDate',
      render: (val) => formatDate(val, 'YYYY-MM-DD') as string,
    },
    {
      label: '审理程序',
      field: 'trialProcedure',
    },
    {
      label: '裁判结果',
      field: 'judgmentResult',
      render: (val) =>
        h('div', {
          innerHTML: val,
          class: 'whitespace-pre-wrap text-sm leading-relaxed',
        }),
    },
    {
      label: '案件类型',
      field: 'caseType',
    },
    {
      label: '案件来源',
      field: 'caseSource',
    },
    {
      label: '法院级别',
      field: 'caseLevel',
    },
    {
      label: '状态',
      field: 'status',
      render: (val) =>
        h(DictTag, {
          options: [
            {
              label: '归档',
              value: '0',
            },
            {
              label: '有效',
              value: '1',
            },
          ],
          value: val,
        }),
    },
    {
      label: '是否已公开',
      field: 'isPublished',
      render: (val) =>
        h(DictBadge, {
          value: val,
        }),
    },
    {
      label: '是否向量化',
      field: 'vectorProcessed',
      render: (val) =>
        h(DictBadge, {
          value: val,
        }),
    },
    {
      label: 'ai处理状态',
      field: 'aiProcessStatus',
      render: (val) =>
        h(DictTag, {
          options: [
            {
              label: '待处理',
              value: '0',
            },
            {
              label: '处理中',
              value: '1',
            },
            {
              label: '处理完成',
              value: '2',
            },
            {
              label: '处理失败',
              value: '3',
            },
          ],
          value: val,
        }),
    },
    {
      label: 'ai处理时间',
      field: 'aiProcessTime',
      render: (val) => formatDateTime(val) as string,
    },
    {
      label: 'AI分析置信度',
      field: 'aiConfidence',
    },
    {
      label: 'AI自动分类标签',
      field: 'aiCategoryTags',
      render: (val) =>
        h('div', {
          innerHTML: val,
          class: 'whitespace-pre-wrap text-sm leading-relaxed',
        }),
    },
    {
      label: 'AI关键词提取',
      field: 'aiKeywords',
      render: (val) =>
        h('div', {
          innerHTML: val,
          class: 'whitespace-pre-wrap text-sm leading-relaxed',
        }),
    },
    {
      label: 'AI提取的关键句',
      field: 'aiKeySentences',
      render: (val) =>
        h('div', {
          innerHTML: val,
          class: 'whitespace-pre-wrap text-sm leading-relaxed',
        }),
    },
    {
      label: 'AI自动摘要',
      field: 'aiAutoSummary',
      render: (val) =>
        h('div', {
          innerHTML: val,
          class: 'whitespace-pre-wrap text-sm leading-relaxed',
        }),
    },
    {
      label: 'AI争议焦点关键字',
      field: 'aiDispiteKeywords',
      render: (val) =>
        h('div', {
          innerHTML: val,
          class: 'whitespace-pre-wrap text-sm leading-relaxed',
        }),
    },
    {
      label: 'AI法律条文推荐',
      field: 'aiLawArticleRecommend',
      render: (val) =>
        h('div', {
          innerHTML: val,
          class: 'whitespace-pre-wrap text-sm leading-relaxed',
        }),
    },
    {
      label: 'AI风险评估等级',
      field: 'aiRiskLevel',
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'naturalYear',
      label: '所属年度',
      component: 'DatePicker',
      rules: 'required',
      componentProps: {
        picker: 'year',
        format: 'YYYY',
        valueFormat: 'YYYY',
      },
    },
    {
      fieldName: 'caseType',
      label: '案件类型',
      component: 'Select',
      rules: 'required',
      componentProps: {
        allowClear: true,
        options: [
          {
            label: '民事',
            value: '民事',
          },
          {
            label: '刑事',
            value: '刑事',
          },
          {
            label: '行政',
            value: '行政',
          },
          {
            label: '执行',
            value: '执行',
          },
        ],
        placeholder: '请选择案件类型（民事/刑事/行政/执行）',
      },
    },
    {
      fieldName: 'naturalYm',
      label: '所属年月',
      component: 'DatePicker',
      componentProps: {
        picker: 'month',
        format: 'YYYYMM',
        valueFormat: 'YYYYMM',
      },
    },
    {
      fieldName: 'caseNo',
      label: '案号',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入案号',
      },
    },
    {
      fieldName: 'caseCause',
      label: '案由',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入案由',
      },
    },
    {
      fieldName: 'caseCauseCode',
      label: '案由编码（依据最高法案由规定）',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入案由编码（依据最高法案由规定）',
      },
    },
    {
      fieldName: 'courtName',
      label: '审理法院',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入审理法院',
      },
    },
    {
      fieldName: 'courtCode',
      label: '法院编码',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入法院编码',
      },
    },
    {
      fieldName: 'judgmentDate',
      label: '裁判时间',
      component: 'DatePicker',
      componentProps: {
        showTime: false,
        format: 'YYYY-MM-DD',
        valueFormat: 'YYYY-MM-DD',
      },
    },
    {
      fieldName: 'trialProcedure',
      label: '审理程序',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: [
          {
            label: '一审程序',
            value: '一审程序',
          },
          {
            label: '二审程序',
            value: '二审程序',
          },
          {
            label: '再审程序',
            value: '再审程序',
          },
          {
            label: '特别程序',
            value: '特别程序',
          },
          {
            label: '督促程序',
            value: '督促程序',
          },
          {
            label: '死刑复核',
            value: '死刑复核',
          },
          {
            label: '速裁程序',
            value: '速裁程序',
          },
          {
            label: '非诉执行审查',
            value: '非诉执行审查',
          },
          {
            label: '首次执行',
            value: '首次执行',
          },
          {
            label: '恢复执行',
            value: '恢复执行',
          },
          {
            label: '财产保全执行',
            value: '财产保全执行',
          },
          {
            label: '执行异议',
            value: '执行异议',
          },
        ],
        placeholder: '请输入审理程序',
      },
    },

    {
      fieldName: 'caseSource',
      label: '案件来源',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: [
          {
            label: '裁判文书网',
            value: '裁判文书网',
          },
          {
            label: '法院系统',
            value: '法院系统',
          },
        ],
        placeholder: '请选择案件来源（裁判文书网/法院系统）',
      },
    },
    {
      fieldName: 'caseLevel',
      label: '法院级别',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: [
          {
            label: '最高法',
            value: '最高法',
          },
          {
            label: '高院',
            value: '高院',
          },
          {
            label: '中院',
            value: '中院',
          },
          {
            label: '基层',
            value: '基层',
          },
        ],
        placeholder: '请选择法院级别（最高法/高院/中院/基层）',
      },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: [
          {
            label: '归档',
            value: '0',
          },
          {
            label: '有效',
            value: '1',
          },
        ],
        placeholder: '请选择状态',
      },
    },
    {
      fieldName: 'isPublished',
      label: '是否已公开',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: [
          {
            label: '已公开',
            value: true,
          },
          {
            label: '未公开',
            value: false,
          },
        ],
        placeholder: '请选择是否已公开',
      },
    },
    {
      fieldName: 'aiProcessStatus',
      label: 'ai处理状态',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: [
          {
            label: '待处理',
            value: '0',
          },
          {
            label: '处理中',
            value: '1',
          },
          {
            label: '处理完成',
            value: '2',
          },
          {
            label: '处理失败',
            value: '3',
          },
        ],
        placeholder: '请选择ai处理状态',
      },
    },
    {
      fieldName: 'createTime',
      label: '创建时间',
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        allowClear: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<LawCaseCoreApi.CaseCore>['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: '主键ID',
      minWidth: 120,
      visible: false,
    },
    {
      field: 'naturalYear',
      title: '所属年度',
      minWidth: 120,
    },
    {
      field: 'naturalYm',
      title: '所属年月',
      minWidth: 120,
    },
    {
      field: 'caseNo',
      title: '案号',
      minWidth: 120,
    },
    {
      field: 'caseCause',
      title: '案由',
      minWidth: 120,
    },
    {
      field: 'caseCauseCode',
      title: '案由编码',
      minWidth: 120,
    },
    {
      field: 'courtName',
      title: '审理法院',
      minWidth: 120,
    },
    {
      field: 'courtCode',
      title: '法院编码',
      minWidth: 120,
    },
    {
      field: 'judgmentDate',
      title: '裁判时间',
      minWidth: 120,
    },
    {
      field: 'trialProcedure',
      title: '审理程序',
      minWidth: 120,
    },
    {
      field: 'judgmentResult',
      title: '裁判结果',
      minWidth: 120,
    },
    {
      field: 'caseType',
      title: '案件类型',
      minWidth: 120,
    },
    {
      field: 'caseSource',
      title: '案件来源',
      minWidth: 120,
    },
    {
      field: 'caseLevel',
      title: '法院级别',
      minWidth: 120,
    },
    {
      field: 'status',
      title: '状态',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: {
          options: [
            {
              label: '归档',
              value: '0',
            },
            {
              label: '有效',
              value: '1',
            },
          ],
        },
      },
    },
    {
      field: 'isPublished',
      title: '是否已公开',
      minWidth: 120,
      cellRender: {
        name: 'CellDictBadge',
      },
    },
    {
      field: 'vectorProcessed',
      title: '是否向量化',
      minWidth: 120,
      cellRender: {
        name: 'CellDictBadge',
      },
    },
    {
      field: 'vectorDbId',
      title: '向量数据库ID',
      minWidth: 120,
    },
    {
      field: 'aiProcessStatus',
      title: 'ai处理状态',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: {
          options: [
            {
              label: '待处理',
              value: '0',
            },
            {
              label: '处理中',
              value: '1',
            },
            {
              label: '处理完成',
              value: '2',
            },
            {
              label: '处理失败',
              value: '3',
            },
          ],
        },
      },
    },
    {
      field: 'aiProcessTime',
      title: 'ai处理时间',
      minWidth: 120,
      formatter: 'formatDateTime',
    },
    {
      field: 'aiConfidence',
      title: 'AI分析置信度',
      minWidth: 120,
    },
    {
      field: 'aiCategoryTags',
      title: 'AI自动分类标签',
      minWidth: 120,
    },
    {
      field: 'aiKeywords',
      title: 'AI关键词提取',
      minWidth: 120,
    },
    {
      field: 'aiKeySentences',
      title: 'AI提取的关键句',
      minWidth: 120,
    },
    {
      field: 'aiAutoSummary',
      title: 'AI自动摘要',
      minWidth: 120,
    },
    {
      field: 'aiDispiteKeywords',
      title: 'AI争议焦点关键字',
      minWidth: 120,
    },

    {
      field: 'aiLawArticleRecommend',
      title: 'AI法律条文推荐',
      minWidth: 120,
    },
    {
      field: 'aiRiskLevel',
      title: 'AI风险评估等级',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: {
          options: [
            {
              label: '无风险',
              value: '0',
            },
            {
              label: '低风险',
              value: '1',
            },
            {
              label: '中风险',
              value: '2',
            },
            {
              label: '高风险',
              value: '3',
            },
          ],
        },
      },
    },
    {
      field: 'createTime',
      title: '创建时间',
      minWidth: 120,
      formatter: 'formatDateTime',
    },
    {
      field: 'actions',
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
